import Axios from './axios'
import { getFilename } from './util'

function Http(options) {
    const http = Axios(options)

    function get(url, params, headers) {
        return http({
            url,
            method: 'get',
            params,
            headers,
        })
    }
    
    function post(url, data, params, headers) {
        return http({
            url,
            method: 'post',
            data,
            params,
            headers,
        })
    }

    function postWWW(url, data, params) {
        return post(url, data, params, {
            'Content-Type': 'application/x-www-form-urlencoded'
          })
    }

    function postJson(url, data, params) {
        return post(url, data, params, {
            'Content-Type': 'application/json'
          })
    }

    function postForm(url, data, params) {
        return post(url, data, params, {
            'Content-Type': 'multipart/form-data'
          })
    }
    
    function put(url, data, params, headers) {
        return http({
            url,
            method: 'put',
            data,
            params,
            headers,
        })
    }
    
    function deleteAction(url, data, params, headers) {
        return http({
            url,
            method: 'delete',
            data,
            params,
            headers
        })
    }
    
    // **********进阶************
    
    function getBlob(url, params, headers) {
        return http({
            url,
            method: 'get',
            params,
            responseType: 'blob',
            headers: headers || {}
        })
    }
    
    function postBlob(url, data, params, headers) {
        return http({
            url,
            method: 'post',
            params,
            data,
            responseType: 'blob',
            headers: headers || {}
        })
    }
    
    function download(url, params, type = 'get', nameOrKey, headers = {}) {
        return new Promise((resolve, reject) => {
            const action = String(type).toLocaleLowerCase() === 'get' ?
                getBlob(url, params, headers) :
                postBlob(url, params, {}, headers)
            action
                .then(data => {
                    if (!data || data.size === 0) {
                        reject(new Error('文件下载失败'))
                        return
                    }
                    if (!data instanceof Blob) {
                        data = new Blob([data])
                    }
                    let fileName = ''
                    if (String(nameOrKey).includes('.')) {
                        fileName = nameOrKey
                    } else {
                        fileName = getFilename(url, nameOrKey)
                        if (!fileName) return reject(new Error('请传入正确的名称参数'))
                    }
                    if (typeof window.navigator.msSaveBlob !== 'undefined') {
                        window.navigator.msSaveBlob(data, fileName)
                    } else {
                        let url = window.URL.createObjectURL(data)
                        let link = document.createElement('a')
                        link.style.display = 'none'
                        link.href = url
                        link.setAttribute('download', fileName)
                        document.body.appendChild(link)
                        link.click()
                        document.body.removeChild(link) //下载完成移除元素
                        window.URL.revokeObjectURL(url) //释放掉blob对象
                    }
                    resolve(data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    }
    

    return {
        http,
        get,
        post,
        postWWW,
        postForm,
        postJson,
        put,
        deleteAction,
        getBlob,
        postBlob,
        download,
    }
}

export default Http